<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Cache;

class Ad extends BasicAdmin
{
    public $table = 'Ad';

    /**
     * 数据列表展示
     * @return \think\response\View
     */
    public function index()
    {
        $res = Db::name('AdPosition')->where('status', 1)->select();
        return view('',['positions'=>$res]);
    }

    /**
     * 获取列表数据
     * @return \think\response\Json
     */
    public function getList(){
        $get_datas = $this->request->get();
        $where = [];

        $db = Db::name($this->table);

        if(isset($get_datas['name']) && $get_datas['name']) {
            $where[] = ['name', 'like', '%'.$get_datas['name'].'%'];
        }

        if(isset($get_datas['position_id']) && $get_datas['position_id']) {
            $where[] = ['position_id', '=', $get_datas['position_id']];
        }

        $res  = DataService::getData($db, $get_datas['limit']??10, $where);

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return json($data);
    }

    /**
     * 添加信息
     * @return \think\response\View
     */
    public function create()
    {
        $res = Db::name('AdPosition')->where('status', 1)->select();
        return view('', ['positions' => $res]);
    }

    /**
     * 保存信息
     * @return \think\response\Json
     */
    public function store()
    {
        $data = $this->request->post();
        $data['create_time'] = date('Y-m-d H:i:s');

        $article_id = DataService::save(Db::name($this->table), $data, 'id', []);

        $data = [
            'code' => 200,
            'msg'   => '添加成功',
        ];
        return json($data);
    }

    /**
     * 修改信息页面
     * @param int $id  信息的主键id
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        $res = Db::name($this->table)->where('id',$id)->find();
        $data['data'] = $res;
        $res = Db::name('AdPosition')->where('status', 1)->select();
        $data['positions'] = $res;
        return view('',$data);
    }

    /**
     * 保存修改信息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update()
    {
        $data = $this->request->post();

        $id = $data['id'];
        $res = Db::name($this->table)->where('id',$id)->update($data);

        if($res) {
            $data = [
                'code' => 200,
                'msg'   => '更新成功',
            ];
        } else {
            $data = [
                'code' => 0,
                'msg'   => '更新失败',
            ];
        }

        return json($data);
    }

    /**
     * 删除信息 ids 【批量删除】
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function destroy()
    {
        $ids = $this->request->post();
        if (empty($ids['ids'])){
            return json(['code'=>0,'msg'=>'请选择删除项']);
        }
        $res = Db::name($this->table)->whereIn('id', $ids['ids'])->delete();
        if($res) {
            $data = [
                'code' => 200,
                'msg'   => '删除成功',
            ];
        } else {
            $data = [
                'code' => 0,
                'msg'   => '删除失败',
            ];
        }

        return json($data);
    }


    public function updateAct(){
        $data = $this->request->post();
        $res = Db::name($this->table)->where('id', $data['id'])->update([$data['act'] => $data['value']]);
        if($res) {
            $data = [
                'code' => 200,
                'msg'   => '更新成功',
            ];
        } else {
            $data = [
                'code' => 0,
                'msg'   => '更新失败',
            ];
        }
        return json($data);
    }

    public function uploads(){
        return uploads();
    }
}
